package binary_evolution;

import evolution_superclasses.Genetic_Operators;
import evolution_superclasses.Genotype;

public class Binary_Genetic_Operators extends Genetic_Operators {

	public Binary_Genetic_Operators(int size) {
		super(size);
	}

	@Override
	public void crossOver(Genotype gen1, Genotype gen2) {
		int crossOverPoint = _rand.nextInt(_genSize);
		Binary_Genotype bg1 = (Binary_Genotype)gen1;
		Binary_Genotype bg2 = (Binary_Genotype)gen2;

		boolean[] array1 = bg1.get_binaryArray();
		boolean[] array2 = bg2.get_binaryArray();

		int i;
		for(i = 0; i < crossOverPoint; i++){
			bg1.editBinaryArray(i, array1[i]);
			bg2.editBinaryArray(i, array2[i]);
		}
		for(;i < _genSize; i++){
			boolean tmp = array1[i];
			bg1.editBinaryArray(i, array2[i]);
			bg2.editBinaryArray(i, tmp);
		}
	}

	@Override
	public void mutation(Genotype gen) {
		int i = _rand.nextInt(_genSize);
		((Binary_Genotype)gen).editBinaryArray(i, !((Binary_Genotype)gen).get_binaryArray()[i]);
	}

}
